package meatslope2.application;

import javax.microedition.khronos.opengles.GL10;

import meatslope2.gameengine.engine.Camera;
import meatslope2.gameengine.engine.Engine;
import meatslope2.gameengine.input.InputSystem;
import meatslope2.gameengine.scene.Scene;
import android.util.Log;

//TODO: make a lot of functions final
//TODO: motion blur
//TODO: maybe set up pools of primitive types that you can use

//TODO:
//shooting buttons - next
//shooting
//animation
//draw right button relative to screen
//only render stuff within camera view
//get rid of bitmap constants class
//refactor how not drawing relative to camera is handled
//put final in arguments
//possible get rid of circle/Ishape altogether
//refactor buttons (after sleep)

public class Game extends Engine
{
	// ===========================================================
	// Members
	// ===========================================================
	private final Scene scene;
	private final InputSystem.HUD hud;
	private final Camera camera;
	
	//For debugging purposes
	private int currentSecond = 0;
	private int numFrames = 0;
	
	// ===========================================================
	// Getters/Setters
	// ===========================================================
	
	public final Camera getCamera()
	{
		return this.camera;
	}
	
	// ===========================================================
	// Constructors
	// ===========================================================
	public Game(Scene scene, InputSystem inputSystem)
	{	
		super(inputSystem);
		
		assert scene != null : "scene argument cannot be null";
		assert inputSystem != null : "input system argument cannot be null";
		assert inputSystem.getHUD() != null : "input system's HUD cannot be null";
		
		this.scene = scene;
		this.hud = inputSystem.getHUD();
		this.camera = new Camera(this, scene.getPlayer());
	}
	
	// ===========================================================
	// Methods for/from SuperClass/Interfaces
	// ===========================================================
	/**
	 * Main update cycle
	 * @throws InterruptedException
	 */
	@Override
	protected void update() throws InterruptedException 
	{		
		super.update();
		
		this.camera.update();
		this.scene.update();
		
		if(SystemRegistry.DEBUG_MODE)
			logFrameRate();
	}
	
	/**
	 * Main draw cycle
	 * @throws InterruptedException
	 */
	@Override
	protected void draw(final GL10 pGL) throws InterruptedException
	{
		//pGL.glOrthof(camera.getCenter().x, camera.getCenter().x + getWidth(), 0.0f, getHeight(), 0.0f, 1.0f);
		
		this.scene.draw(pGL);
		this.hud.draw(pGL);
	}

	@Override
	protected void consumeEvents() throws InterruptedException 
	{
		inputSystem.consumeEvents();
	}

	// ===========================================================
	// Methods
	// ===========================================================
	private void logFrameRate()
	{
		numFrames++;
		
		if(currentSecond + 1 < Time.getTotalTime())
		{
			Log.d("FPS", "FPS: " + String.format("%d", numFrames));
			if(numFrames < 55)
				Log.e("FPS", "Below 55fps");
			
			currentSecond++;
			numFrames = 0;
		}
	}
}
